import {
  userPage,
  userInsert,
  userDel,
  userEdit,
  rolesData,
  saveUserRoles,
} from '../services/user'
import {message} from "antd";

export default {
  namespace: 'userList',

  state: {
    addForm: {},
    searchForm: {},
    modalForm: {},
    list: [],
    selection: {},
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    },
    modalVisible: false,
    optFlag: '',
    compOption: [],
    transferVisible: false,
    transferTitle: '',
    rolesData: [],
    activeUserId: '',
    rolesTargetKeys: []
  },

  effects: {
    * page({payload}, {call, put}) {
      const res = yield call(userPage, payload);
      if (res && res.code === 0) {
        const list = res.result.records;
        const pagination = {
          pages: res.result.pages,
          pageSize: res.result.size,
          total: res.result.total,
          current: res.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        const data = {list: list, pagination: pagination};
        yield put({
          type: 'page_r',
          payload: data
        })
      }
    },

    * insert({payload}, {call, put}) {
      const {fields, searchForm, pagination} = payload;
      const response = yield call(userInsert, fields);
      if (response && response.code === 0) {
        message.success('添加成功');
        yield put({
          type: "page",
          payload: {...searchForm,...pagination}
        });
        yield put({
          type: "handleCleanAddForm"
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
      }
    },

    * update({payload}, {call, put}) {
      const {fields, searchForm, pagination} = payload;
      const res = yield call(userEdit, fields);
      if (res && res.code === 0) {
        message.success('修改成功');
        yield put({
          type: "page",
          payload: {...searchForm,...pagination}
        });
        yield put({
          type: "handleTableSelection",
          payload: fields
        });
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        });
      }
    },

    * del({payload}, {call, put}) {
      const {fields, searchForm, pagination} = payload;
      const res = yield call(userDel, fields);
      if (res && res.code === 0) {
        message.success('删除成功');
        yield put({
          type: "page",
          payload: {...searchForm,...pagination}
        })
        yield put({
          type: "handleTableSelection",
          payload: {}
        })
        yield put({
          type: 'handleModalVisible',
          payload: {modalVisible: false}
        })
        yield put({
          type: 'getCompOptions'
        })
      };
    },

    * getTransfer({payload}, {call, put}) {
      const res = yield call(rolesData, payload);
      if (res && res.code === 0) {

        yield put({
          type: 'putTransfer',
          payload: res.result
        });
      }
    },

    * saveUserRoles({payload}, {call, put}) {
      const res = yield call(saveUserRoles, payload);
      if (res && res.code === 0) {
        message.success('保存成功');
        yield put({
          type: 'handleTransferVisible',
          payload: false
        })
      }
    }
  },

  reducers: {
    page_r(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
    putTransfer(state, action){
      const userRoles = action.payload;
      let rolesTargetKeys = [];
      userRoles.map(item => {
        if(item.chosen){
          rolesTargetKeys.push(item.key);
        }
      })
      return {
        ...state,
        rolesData: userRoles,
        rolesTargetKeys: rolesTargetKeys
      }
    },
    handleModalVisible(state, action) {
      return {
        ...state,
        ...action.payload
      }
    },
    handleSearchChange(state, action) {
      const {searchForm} = state;
      const newSearchForm = {
        ...searchForm,
        ...action.payload
      }
      return {
        ...state,
        searchForm: newSearchForm
      }
    },
    handleModalChange(state, action) {
      const {modalForm} = state;
      const {fields, optFlag} = action.payload;
      const newModalForm = {
        ...modalForm,
        ...fields
      }

      let st = {
        ...state,
        modalForm: newModalForm
      }
      if(optFlag === "add"){
        let { addForm }= action.payload
        Object.keys(fields).map(key => {
          addForm[key] = fields[key]['value'];
        })
        st = {
          ...st,
          addForm: addForm
        }
      }
      return st
    },
    handleTableSelection(state, action) {
      return {
        ...state,
        selection: action.payload
      }
    },
    handleCleanAddForm(state) {
      return {
        ...state,
        addForm: {}
      }
    },
    handleTransferChange (state, action) {
      return{
        ...state,
        rolesTargetKeys: action.payload
      }
    },
    handleTransferVisible(state, action) {
      return{
        ...state,
        transferVisible: action.payload
      }
    },
    handleTransferTitleChange(state, action) {
      return{
        ...state,
        transferTitle: action.payload
      }
    },
    handleActiveUserChange(state, action) {
      return{
        ...state,
        activeUserId: action.payload
      }
    }

  },
}
